<template>
  <div class="password">
    <NavBar title="重置密码" back/>
    <div class="logo tb-logo"></div>
    <div class="password-form">
      <div class="password-boder">
        <div class="password-icon password-phone"></div>
        <input class="password-input" type="text" v-model="cellphone"  @change="checkTel" id="cellphone" placeholder="手机号码"/>
      </div>
      <div class="password-boder">
        <div class="password-icon password-code"></div>
        <input class="password-input password-codes" type="number" v-model="code" @change="checkCode" id="code" placeholder="短信验证码"/>
        <div class="password-send" v-text="msg" @click="handelSend">发送验证码</div>
      </div>
      <div class="password-boder">
        <div class="password-icon password-password"></div>
        <input class="password-input password-pwd" :type="type" v-model="password" @change="checkPassword" id="password" placeholder="登录密码"/>
        <div class="password-hide" :class="{'password-show':display}" @click="handelShow"></div>
      </div>
      <div class="password-btn" @click="resetPassword">立即重置</div>
    </div>
  </div>
</template>
<script>
import NavBar from '../../components/common/NavBar.vue'
import { Toast } from 'vant'
import passwordApi from '../../api/user'
export default {
  name: 'password',
  data () {
    return {
      cellphone: '',
      code: '',
      password: '',
      type: 'password',
      display: false,
      send: false,
      msg: '发送验证码',
      cdisabel: false
    }
  },
  components: {
    NavBar
  },
  methods: {
    checkTel: function () {
      if (this.cellphone === '') {
        Toast('手机号码不能为空')
      } else {
        let reg = /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])[0-9]{8}$/
        if (!reg.test(this.cellphone)) {
          Toast('请输入格式正确的电话号码')
        }
      }
    },
    checkCode: function () {
      if (this.code === '') {
        Toast('验证码不能为空')
      }
    },
    checkPassword: function () {
      if (this.password.length < 6 || this.password.length > 10) {
        Toast('密码长度须在6-10位之间')
      }
    },
    handelShow: function () {
      if (!this.display) {
        this.type = 'text'
      } else {
        this.type = 'password'
      }
      this.display = !this.display
    },
    handelSend: function () {
      if (this.cellphone === '') {
        Toast('手机号码不能为空')
        return false
      } else {
        let reg = /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])[0-9]{8}$/
        if (!reg.test(this.cellphone)) {
          Toast('请输入格式正确的电话号码')
          return false
        }
      }
      if (this.cdisabel) {
        return 0
      }
      const _this = this
      passwordApi.passwordCode(this.cellphone).then(function (res) {
        if (res.data.success) {
          if (!_this.send) {
            let sec = 60
            _this.cdisabel = true
            for (let i = 0; i <= 60; i++) {
              window.setTimeout(function () {
                if (sec !== 0) {
                  _this.msg = sec + '秒后重发'
                  sec--
                } else {
                  _this.cdisabel = false
                  _this.msg = '发送验证码'
                }
              }, i * 1000)
            }
          }
          Toast.success(res.data.re)
        } else {
          _this.cdisabel = false
          Toast(res.data.msg)
        }
      })
    },
    resetPassword: function () {
      if (this.cellphone === '') {
        Toast('手机号码不能为空')
        return false
      } else {
        let reg = /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])[0-9]{8}$/
        if (!reg.test(this.cellphone)) {
          Toast('请输入格式正确的电话号码')
          return false
        }
      }
      if (this.code === '') {
        Toast('手机验证码不能为空')
        return false
      } else if (this.password.length < 6 || this.password.length > 10) {
        Toast('密码长度须在6-10位之间')
        return false
      }
      const _this = this
      passwordApi.memberPassword(this.cellphone, this.code, this.password).then(function (res) {
        if (res.data.success) {
          Toast.success(res.data.re)
          _this.$router.push('/login')
        } else {
          Toast(res.data.msg)
        }
      })
    }
  }
}
</script>
<style>
  .password {
    position: absolute;
    height: 100%!important;
    background: #fff;
  }
  .logo {
    width: 60px;
    height: 60px;
    background: url('../../assets/logo.png') no-repeat;
    background-size: 60px;
    margin: 40px auto;
  }

  .password-inp label {
    width: 4em;
    text-align: center;
    display: inline-block;
    color: #1ABC9C;
    font-size: 14px;
  }

  .password-txt {
    text-align: center;
    color: #1ABC9C;
    font-size: 12px;
    margin-top: 30px;
  }

  .password-txt a {
    color: #1ABC9C;
    padding: 0 5px;
  }

  .password-boder {
    width: 80%;
    height: 35px;
    margin-top: 10px;
    margin-left: 10%;
    text-align: center;
    border-bottom: 1px solid #1ABC9C;
  }

  .password-icon {
    height: 28px;
    line-height: 28px;
    width: 10%;
    float: left;
    text-align: right;
  }

  .password-phone {
    background-image: url('../../../static/images/login/cellphone.png');
    background-repeat: no-repeat;
    background-size: 20px;
    background-position: 5px 7px;
  }

  .password-password {
    background-image: url('../../../static/images/login/password.png');
    background-repeat: no-repeat;
    background-size: 20px;
    background-position: 5px 7px;
  }

  .password-code {
    background-image: url('../../../static/images/login/code.png');
    background-repeat: no-repeat;
    background-size: 20px;
    background-position: 5px 7px;
  }

  .password-input {
    width: 80%;
    float: left;
    margin-left: 6px;
    height: 28px;
    line-height: 28px;
    border: 0px;
    color: #333333;
    font-size: 14px;
  }

  .password-pwd {
    width: 70%;
  }

  .password-hide {
    height: 28px;
    line-height: 28px;
    width: 10%;
    float: right;
    text-align: right;
    background-image: url('../../../static/images/login/hide.png');
    background-repeat: no-repeat;
    background-size: 20px;
    background-position: 5px 7px;
  }

  .password-show {
    background-image: url('../../../static/images/login/show.png');
  }

  .password-input::-webkit-input-placeholder {
    color: #cccccc;
    font-size: 12px;
  }

  .password-btn {
    width: 80%;
    height: 40px;
    line-height: 40px;
    margin-left: 10%;
    text-align: center;
    margin-top: 30px;
    font-size: 14px;
    font-weight: bold;
    border-radius: 5px;
    color: #ffffff;
    display: inline-block;
    background-color: #1ABC9C;
  }

  .read-check {
    display: inline-block;
    vertical-align: middle;
    margin-right: 5px;
  }

  .password-codes {
    width: 50%;
  }

  .password-send {
    width: 30%;
    height: 37px;
    line-height: 37px;
    float: right;
    text-align: right;
    font-size: 12px;
    color: #888;
  }

  .read-text {
    color: #888;
  }
</style>
